var isDivHiden = {created: false, appointed: true, historic: true, evaluations: true};

$(document).ready(function () {
    $('#appointedTripsDiv').hide();
    $('#historicDiv').hide();
    $('#evaluationsDiv').hide();
    initCreatedtrip();
    initAppointedtrip();
    initHistoric();
    initEvaluations();
});

function initCreatedtrip() {
    var table = $('#table_createdtrip').DataTable({
        ajax: {
            url: "/Trabalho_PD/webresources/WebServiceBoleias/getAllTripsCreatedBy/" + user_id,
            type: "GET",
            dataSrc: "trip"
        },
        columns: [
            {
                data: function (row, type, val, meta) {
                    if (row.ended) {
                        return true;
                    } else {
                        return false;
                    }
                },
                render: function (data, type, full, meta) {
                    if (data) {
                        return '<span class="positive"><i class="fa fa-check fa-2x"></i></span>';
                    } else {
                        return '<span class="warning"><i class="fa fa-tag fa-2x"></i></span>';
                    }
                }
            },
            {data: 'departueDate'},
            {data: 'arrivalDate'},
            {data: 'departue_address'},
            {data: 'destination_address'},
            {data: 'car_brand'},
            {
                "className": 'details-control',
                "orderable": false,
                "data": null,
                "defaultContent": '<i class="btn btn-info fa fa-search fa-2x"></i>'
            }
        ]

    });

    $('#table_createdtrip tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var data = table.row(tr).data();
        location.href = '/Trabalho_PD/tripinformation.html?id=' + data["id_trip"];
    });
}

function initAppointedtrip() {
    $('#appointedTripsDiv').hide();
    var table = $('#table_appointedtrip').DataTable({
        ajax: {
            url: "/Trabalho_PD/webresources/WebServiceBoleias/getAllAppointedTripsFor/" + user_id,
            type: "GET",
            dataSrc: "trip"
        },
        columns: [
            {
                data: function (row, type, val, meta) {
                    if (row.accepted) {
                        return true;
                    } else {
                        return false;
                    }
                },
                render: function (data, type, full, meta) {
                    if (data) {
                        return '<span class="positive"><i class="fa fa-check fa-2x"></i></span>';
                    } else {
                        return '<span class="negative"><i class="fa fa-times fa-2x"></i></span>';
                    }
                }
            },
            {data: 'departueDate'},
            {data: 'arrivalDate'},
            {data: 'departue_address'},
            {data: 'destination_address'},
            {data: 'car_brand'},
            {
                "className": 'details-control',
                "orderable": false,
                "data": null,
                "defaultContent": '<i class="btn btn-info fa fa-search fa-2x"></i>'
            }
        ]

    });

    $('#table_appointedtrip tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var data = table.row(tr).data();
        location.href = '/Trabalho_PD/tripinformation.html?id=' + data["id_trip"];
    });

}

function initHistoric() {
    $('#historicDiv').hide();
    var table = $('#table_historic').DataTable({
        ajax: {
            url: "/Trabalho_PD/webresources/WebServiceBoleias/getAllTripsHistoricFor/" + user_id,
            type: "GET",
            dataSrc: "trip"
        },
        columns: [
            {
                data: function (row, type, val, meta) {
                    if (row.accepted) {
                        return true;
                    } else {
                        return false;
                    }
                },
                render: function (data, type, full, meta) {
                    if (data) {
                        return '<span class="positive"><i class="fa fa-check fa-2x"></i></span>';
                    } else {
                        return '<span class="negative"><i class="fa fa-times fa-2x"></i></span>';
                    }
                }
            },
            {data: 'departueDate'},
            {data: 'arrivalDate'},
            {data: 'departue_address'},
            {data: 'destination_address'},
            {data: 'car_brand'},
            {
                "className": 'details-control',
                "orderable": false,
                "data": null,
                "defaultContent": '<i class="btn btn-info fa fa-search fa-2x"></i>'
            }
        ]

    });

    $('#table_historic tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var data = table.row(tr).data();
        location.href = '/Trabalho_PD/tripinformation.html?id=' + data["id_trip"];
    });
}

function initEvaluations() {
    $('#evaluationsDiv').hide();

    var table = $('#table_evaluations').on('xhr.dt', function (e, settings, json) {
        $('s').insertAfter("#evaluationsButton");
    }).DataTable({
        ajax: {
            url: "/Trabalho_PD/webresources/WebServiceBoleias/getAllMissingEvaluationsFor/" + user_id,
            type: "GET",
            dataSrc: "trip"
        },
        columns: [
            {data: 'departueDate'},
            {data: 'arrivalDate'},
            {data: 'departue_address'},
            {data: 'destination_address'},
            {data: 'car_brand'},
            {
                "className": 'details-control',
                "orderable": false,
                "data": null,
                "defaultContent": '<i class="btn btn-info fa fa-search fa-2x"></i>'
            },
            {
                "className": 'edit-control',
                "orderable": false,
                "data": null,
                "defaultContent": '<i class="btn btn-success fa fa-check-circle fa-2x"></i>'
            }
        ]
    });
    
    $('#table_evaluations tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var data = table.row(tr).data();
        location.href = '/Trabalho_PD/tripinformation.html?id=' + data["id_trip"];
    });
    
    $('#table_evaluations tbody').on('click', 'td.edit-control', function () {
        var tr = $(this).closest('tr');
        var data = table.row(tr).data();
        location.href = '/Trabalho_PD/evaluate.html?id=' + data["id_trip"];
    });
}

function onDoHide() {
    if (isDivHiden.created) {
        $('#createdTripsDiv').hide(2000);
    } else {
        $('#createdTripsDiv').show(1000);
    }
    if (isDivHiden.appointed) {
        $('#appointedTripsDiv').hide(2000);
    } else {
        $('#appointedTripsDiv').show(1000);
    }
    if (isDivHiden.historic) {
        $('#historicDiv').hide(2000);
    } else {
        $('#historicDiv').show(1000);
    }
    if (isDivHiden.evaluations) {
        $('#evaluationsDiv').hide(2000);
    } else {
        $('#evaluationsDiv').show(1000);
    }
}

function onSeeCreatedTrips() {
    isDivHiden.created = false;
    isDivHiden.appointed = true;
    isDivHiden.historic = true;
    isDivHiden.evaluations = true;
    onDoHide();
}
function onSeeAppointedTrips() {
    isDivHiden.created = true;
    isDivHiden.appointed = false;
    isDivHiden.historic = true;
    isDivHiden.evaluations = true;
    onDoHide();
}
function onSeeHistoric() {
    isDivHiden.created = true;
    isDivHiden.appointed = true;
    isDivHiden.historic = false;
    isDivHiden.evaluations = true;
    onDoHide();
}
function onSeeEvaluations() {
    isDivHiden.created = true;
    isDivHiden.appointed = true;
    isDivHiden.historic = true;
    isDivHiden.evaluations = false;
    onDoHide();
}

function createEvaluateHtml() {
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
            '<tr>' +
            '<td>Welcome message:</td>' +
            '<td>sa</td>' +
            '</tr>' +
            '<tr>' +
            '<td><button type="button" onclick="onActivateWaitingAccountClick(\'\')" class="btn btn-primary"><i class="fa fa-check fa-2x"></i></button></td>' +
            '</tr>' +
            '</table>';
}